package com.zhilianbao.leyaogo.utils;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import static android.R.attr.animation;

/**
 * ClassName: AnimationUtils <br>
 * Description: 动画工具类<br>
 * Author: Cyarie <br>
 * Created: 2017/1/16 09:53 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class AnimationUtils {

    public static final int Default = -1;


    public static final int Linear = 0;
    public static final int Accelerate = 1;
    public static final int Decelerate = 2;
    public static final int AccelerateDecelerate = 3;
    public static final int Bounce = 4;
    public static final int Overshoot = 5;
    public static final int Anticipate = 6;
    public static final int AnticipateOvershoot = 7;


    public static final int DIRECTION_LEFT = 0;
    public static final int DIRECTION_TOP = 1;
    public static final int DIRECTION_RIGHT = 2;
    public static final int DIRECTION_BOTTOM = 3;


    /**
     * 设置动画插值器 动画时间  动画延时
     *
     * @param animation
     * @param interpolatorType
     * @param durationMillis
     * @param delayMillis
     */
    private static void setEffect(Animation animation, int interpolatorType, long durationMillis, long delayMillis) {
        switch (interpolatorType) {
            case Linear:
                animation.setInterpolator(new LinearInterpolator());
                break;
            case Accelerate:
                animation.setInterpolator(new AccelerateInterpolator());
                break;
            case Decelerate:
                animation.setInterpolator(new DecelerateInterpolator());
                break;
            case AccelerateDecelerate:
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                break;
            case Bounce:
                animation.setInterpolator(new BounceInterpolator());
                break;
            case Overshoot:
                animation.setInterpolator(new OvershootInterpolator());
                break;
            case Anticipate:
                animation.setInterpolator(new AnticipateInterpolator());
                break;
            case AnticipateOvershoot:
                animation.setInterpolator(new AnticipateOvershootInterpolator());
                break;
        }
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
    }


    /**
     * 进入动画
     *
     * @param view
     * @param animation
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    private static void baseIn(View view, Animation animation, int interpolator, long durationMillis, long delayMillis) {
        setEffect(animation, interpolator, durationMillis, delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
    }


    /**
     * 出去动画
     *
     * @param view
     * @param animation
     * @param durationMillis
     * @param delayMillis
     */
    private static void baseOut(View view, Animation animation, long durationMillis, long delayMillis) {
        setEffect(animation, Default, durationMillis, delayMillis);
        animation.setAnimationListener(new OutAnimationListener(view));
        view.startAnimation(animation);
    }


    /**
     * 动画监听
     */
    static class OutAnimationListener implements Animation.AnimationListener {

        private View view;

        public OutAnimationListener(View view) {
            this.view = view;
        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            this.view.setVisibility(View.GONE);
            ((ViewGroup) this.view.getParent()).removeView(this.view);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

    /**
     * 淡入动画
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void fadeIn(View view, int interpolator, long durationMillis, long delayMillis) {
        baseIn(view, new AlphaAnimation(0.0f, 1.0f), interpolator, durationMillis, delayMillis);
    }


    /**
     * 淡出动画
     *
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void fadeOut(View view, long durationMillis, long delayMillis) {
        baseOut(view, new AlphaAnimation(1.0f, 0.0f), durationMillis, delayMillis);
    }


    /**
     * 平移进入动画
     *
     * @param directionType
     * @param interpolator
     * @param view
     * @param durationMillis
     * @param delayMillis
     * @param listener
     */
    public static void slideIn(int directionType, int interpolator, View view, long durationMillis, long delayMillis, Animation.AnimationListener listener) {
        if (view == null)
            return;
        Animation animation = null;
        switch (directionType) {
            case DIRECTION_LEFT:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
            case DIRECTION_RIGHT:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
            case DIRECTION_TOP:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
            case DIRECTION_BOTTOM:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
        }
        animation.setAnimationListener(listener);
        baseIn(view, animation, interpolator, durationMillis, delayMillis);
    }


    /**
     * 平移出去动画
     *
     * @param directionType
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void slideOut(int directionType, View view, long durationMillis, long delayMillis) {
        Animation animation = null;
        switch (directionType) {
            case DIRECTION_LEFT:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
            case DIRECTION_RIGHT:
                animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
                break;
        }
        baseOut(view, animation, durationMillis, delayMillis);
    }


    /**
     * 放大进入
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void scaleIn(View view, int interpolator, long durationMillis, long delayMillis) {
        baseIn(view, new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f), interpolator, durationMillis, delayMillis);
    }

    /**
     * 缩小出去出去动画
     *
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void scaleOut(View view, long durationMillis, long delayMillis) {
        baseOut(view, new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f), durationMillis, delayMillis);
    }

    /**
     * 旋转进入动画
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void rotateIn(View view, int interpolator, long durationMillis, long delayMillis) {
        baseIn(view, new RotateAnimation(-90.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f), interpolator, durationMillis, delayMillis);
    }


    /**
     * 旋转出去动画
     *
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void rotateOut(View view, long durationMillis, long delayMillis) {
        baseOut(view, new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f), durationMillis, delayMillis);
    }


    /**
     * 放大旋转进入
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void scaleRotateIn(View view, int interpolator, long durationMillis, long delayMillis) {
        ScaleAnimation animation1 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        RotateAnimation animation2 = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(animation1);
        animation.addAnimation(animation2);
        baseIn(view, animation, interpolator, durationMillis, delayMillis);
    }

    /**
     * 缩小旋转出去
     *
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void scaleRotateOut(View view, long durationMillis, long delayMillis) {
        ScaleAnimation animation1 = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        RotateAnimation animation2 = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(animation1);
        animation.addAnimation(animation2);
        baseOut(view, animation, durationMillis, delayMillis);
    }


    /**
     * 平移淡入
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void slideFadeIn(View view, int interpolator, long durationMillis, long delayMillis) {
        TranslateAnimation animation1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        AlphaAnimation animation2 = new AlphaAnimation(0.0f, 1.0f);
        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(animation1);
        animation.addAnimation(animation2);
        baseIn(view, animation, interpolator, durationMillis, delayMillis);
    }

    /**
     * 平移淡出
     *
     * @param view
     * @param durationMillis
     * @param delayMillis
     */
    public static void slideFadeOut(View view, long durationMillis, long delayMillis) {
        TranslateAnimation animation1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        AlphaAnimation animation2 = new AlphaAnimation(1.0f, 0.0f);
        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(animation1);
        animation.addAnimation(animation2);
        baseOut(view, animation, durationMillis, delayMillis);
    }

    /**
     * 抖动
     *
     * @param view
     * @param interpolator
     * @param durationMillis
     * @param delayMillis
     */
    public static void rotateShake(View view, int interpolator, long durationMillis, long delayMillis) {
        RotateAnimation animation1 = new RotateAnimation(-20.0f, 20.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation1.setRepeatCount(3);
        animation1.setRepeatMode(Animation.REVERSE);
        AnimationSet animation = new AnimationSet(false);
        animation.setFillAfter(false);
        animation.setFillBefore(true);
        animation.addAnimation(animation1);
        setEffect(animation, interpolator, durationMillis, delayMillis);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animation);
    }

}
